import sys
import io
import random
import os

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='gbk', line_buffering=True)
sys.stdout.reconfigure(encoding='gbk')


# 检查输入内容
def checkin():
    if sys.argv[1] == "--help":
        return {
            'status': False,
            'message':
            """
            命令行参数:
            --level    1:随机加减法  2:随机乘除法
            --count    算式的数量
            """
        }
    _d = {}
    try:
        for i in range(len(sys.argv)):
            if sys.argv[i] == '--level':
                _d['level'] = sys.argv[i + 1]
            if sys.argv[i] == '--count':
                _d['count'] = int(sys.argv[i + 1])
    except IndexError as e:
        return {'status': False, 'message': '输入参数不足'}
    except  ValueError as v:
        return {'status': False, 'message': 'count数值错误'}
    except TypeError as t:
        return {'status': False, 'message': 'count类型错误'}

    if (_d['level'] == '1' or _d['level'] == '2') and _d['count'] >= 0:
        return {'status': True, 'data': _d}
    else:
        return {'status': False, 'message': '无效参数'}


def level1():
    _symbol = random.choice(['+', '-'])
    _result = random.randint(30, 99)
    _arg1 = random.randint(int(_result / 3), int(_result / 3 * 2))
    _arg2 = _result - _arg1
    if _symbol == '+':
        return {
            'symbol': _symbol,
            'arg1': _arg1,
            'arg2': _arg2,
            'answer': _result
        }
    else:
        return {
            'symbol': _symbol,
            'arg1': _result,
            'arg2': _arg2,
            'answer': _arg1
        }


def level2():
    # _symbol = random.choice(['×', '÷'])
    _symbol = '×'  # 此处控制乘法还是除法
    _arg1 = random.randint(1, 6) #
    _arg2 = random.randint(1, 6) # 此处2项用于控制乘法口诀内的范围，（1，6）指代只出1-6的口诀
    _result = _arg1 * _arg2
    if _symbol == '×':
        return {
            'symbol': _symbol,
            'arg1': _arg1,
            'arg2': _arg2,
            'answer': _result
        }
    else:
        return {
            'symbol': _symbol,
            'arg1': _result,
            'arg2': _arg2,
            'answer': _arg1
        }


def main(data):
    for i in range(data['count']):
        if data['level'] == '1':
            print(level1())
        elif data['level'] == '2':
            print(level2())


# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':
    _arg = checkin()
    if _arg['status']:
        main(_arg['data'])
    else:
        print(_arg['message'])

# 访问 https://www.jetbrains.com/help/pycharm/ 获取 PyCharm 帮助
